﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.IO;

namespace server
{
    public partial class Img_settings : Form
    {
        private int p_left = 0, p_top = 0, p_height = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height;
        private int p_width = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width, count = 0;
        private IntPtr my_hwnd;
        private activeWindow wnd;
        private Cattura capt = new Cattura();
        private IntPtr[] wnds;
        private Image tmp;
       
        private String path = Path.Combine(Environment.CurrentDirectory, @".\img_config.conf");

        public Img_settings()
        {
            InitializeComponent();
            int i;
            wnd = new activeWindow();
            wnds= wnd.open_wind;
            for (i=0; i< wnds.Length; i++)
            {
                if (wnd.isOk(wnds[i])&&!wnd.getText(wnds[i]).Equals(""))
                {
                    listBoxWindow.Items.Add(wnd.getText(wnds[i]));
                }
                else
                {
                    List<IntPtr> tmp = new List<IntPtr>(wnds);
                    tmp.RemoveAt(i);
                    wnds= tmp.ToArray();
                    i--;
                }
            }

            Bitmap image = new Bitmap(p_width, p_height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            System.Drawing.Graphics GFX = System.Drawing.Graphics.FromImage(image);
            GFX.CopyFromScreen(System.Windows.Forms.Screen.PrimaryScreen.Bounds.X,
                               System.Windows.Forms.Screen.PrimaryScreen.Bounds.Y, 0, 0,
                               System.Windows.Forms.Screen.PrimaryScreen.Bounds.Size,
                               System.Drawing.CopyPixelOperation.SourceCopy);
            img.Image = M_Resize((Image)image, img.Width, img.Height);

            if (File.Exists(path))
            {
                using (StreamReader LeggiConf = File.OpenText(path))
                {
                    String settaggio = LeggiConf.ReadLine();
                    if (settaggio.Equals("desktop"))
                    {
                        Desktop.Checked = true;
                        Window.Checked = false;
                        img.Location = new Point(160, 43);
                        img.Width = 512;
                        img.Height = 400;
                        listBoxWindow.Enabled = false;
                        RightImage.Enabled = false;
                        LeftImage.Enabled = false;
                        SetImage.Enabled = false;
                        scroll_left.Enabled = true;
                        scroll_right.Enabled = true;
                        scroll_top.Enabled = true;
                        scroll_bottom.Enabled = true;
                        scroll_left.Value = int.Parse(LeggiConf.ReadLine());
                        scroll_right.Value = int.Parse(LeggiConf.ReadLine())-9;
                        scroll_top.Value = int.Parse(LeggiConf.ReadLine());
                        scroll_bottom.Value = int.Parse(LeggiConf.ReadLine())-9;
                        p_left = scroll_left.Value * System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width / 100;
                        p_width = (scroll_right.Value - scroll_left.Value) * System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width / 100;
                        p_top = scroll_top.Value * System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height / 100;
                        p_height = (scroll_bottom.Value - scroll_top.Value) * System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height / 100;
                        refresh();
                    }
                    else
                    {
                        Desktop.Checked = false;
                        Window.Checked = true;
                        img.Location = new Point(289, 143);
                        img.Width = 256;
                        img.Height = 200;
                        imgLeft.Image = M_Resize(capt.CaptureWindow(wnds[wnds.Length - 1]), imgLeft.Width, imgLeft.Height);
                        imgRight.Image = M_Resize(capt.CaptureWindow(wnds[1]), imgRight.Width, imgRight.Height);
                        img.Image = M_Resize(capt.CaptureWindow(wnds[0]), img.Width, img.Height);
                        imgLeft.BorderStyle = BorderStyle.Fixed3D;
                        imgRight.BorderStyle = BorderStyle.Fixed3D;
                        img.BorderStyle = BorderStyle.Fixed3D;
                        listBoxWindow.Enabled = true;
                        RightImage.Enabled = true;
                        LeftImage.Enabled = true;
                        SetImage.Enabled = true;
                        scroll_left.Enabled = false;
                        scroll_right.Enabled = false;
                        scroll_top.Enabled = false;
                        scroll_bottom.Enabled = false;
                    }
                }
            }
            else
            {
                
                using (StreamWriter ScriviConf = File.CreateText(path))
                {
                    if (Desktop.Checked)
                    {
                        ScriviConf.WriteLine("desktop");
                        ScriviConf.WriteLine(scroll_left.Value.ToString());
                        ScriviConf.WriteLine(scroll_right.Value.ToString());
                        ScriviConf.WriteLine(scroll_top.Value.ToString());
                        ScriviConf.WriteLine(scroll_bottom.Value.ToString());
                    }
                    else
                    {
                        ScriviConf.WriteLine("window");
                    }
                }
            } 
        }

        private void refresh()
        {
            img.Image = tmp;
            Bitmap image = new Bitmap(p_width, p_height,
                                    System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            System.Drawing.Graphics GFX = System.Drawing.Graphics.FromImage(image);
            GFX.CopyFromScreen(System.Windows.Forms.Screen.PrimaryScreen.Bounds.X+p_left,
                                        System.Windows.Forms.Screen.PrimaryScreen.Bounds.Y+p_top,
                                        0, 0,
                                        new Size(p_width, p_height),
                                        System.Drawing.CopyPixelOperation.SourceCopy);
            img.Image = M_Resize((Image)image, img.Width, img.Height);
            tmp = img.Image;          
        }
    
        private Image M_Resize(Image img, int resizedW, int resizedH)
        {
            Bitmap bmp = new Bitmap(resizedW, resizedH);
            Graphics graphic = Graphics.FromImage((Image)bmp);
            graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
            graphic.DrawImage(img, 0, 0, resizedW, resizedH);
            graphic.Dispose();
            return (Image)bmp;
        }

        private void Reset_Click(object sender, EventArgs e)
        {
            p_height = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height;
            p_width = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width;
            p_top = 0;
            p_left = 0;
            scroll_left.Value = 0;
            scroll_right.Value = 109;
            scroll_top.Value = 0;
            scroll_bottom.Value = 109;
            refresh();
        }

        private void OK_Click(object sender, EventArgs e)
        {
            using (StreamWriter ScriviConf = File.CreateText(path))
            {
                if (Desktop.Checked) 
                {
                    ScriviConf.WriteLine("desktop");
                    ScriviConf.WriteLine(scroll_left.Value.ToString());
                    ScriviConf.WriteLine(scroll_right.Value.ToString());
                    ScriviConf.WriteLine(scroll_top.Value.ToString());
                    ScriviConf.WriteLine(scroll_bottom.Value.ToString());
                }
                else
                {
                    ScriviConf.WriteLine("window");
                }
            }
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        public int gettop
        {
            get { return p_top; }
        }
        
        public int getleft
        {
            get { return p_left; }
        }

        public int getheight
        {
            get { return p_height; }
        }

        public int getwidth
        {
            get { return p_width; }
        }
        public bool window
        {
            get { return Window.Checked; }
        }
        public IntPtr cur_hwnd
        {
            get { return wnds[count]; }
        }
    
        private void RightImage_Click(object sender, EventArgs e)
        {
            
            count++;
            if (count == wnds.Length)
                count = 0;
            my_hwnd = wnds[count];
            img.Image = M_Resize(capt.CaptureWindow(wnds[count]), img.Width, img.Height);
                label5.Text = wnd.getText(wnds[count]);
                listBoxWindow.SetSelected(count, true); 
            if(count==0)
                imgLeft.Image = M_Resize(capt.CaptureWindow(wnds[wnds.Length - 1]), imgLeft.Width, imgLeft.Height);
            else
                imgLeft.Image = M_Resize(capt.CaptureWindow(wnds[count - 1]), imgLeft.Width, imgLeft.Height);
            if(count==wnds.Length-1)
                imgRight.Image = M_Resize(capt.CaptureWindow(wnds[0]), imgRight.Width, imgRight.Height);
            else
                imgRight.Image = M_Resize(capt.CaptureWindow(wnds[count+1]), imgRight.Width, imgRight.Height);
        }

        private void LeftImage_Click(object sender, EventArgs e)
        {
            count--;
            if (count == -1)
                count = wnds.Length - 1;
            my_hwnd = wnds[count]; 
            img.Image = M_Resize(capt.CaptureWindow(wnds[count]), img.Width, img.Height);
            label5.Text = wnd.getText(wnds[count]);
            listBoxWindow.SetSelected(count, true);
            if (count == 0)
                imgLeft.Image = M_Resize(capt.CaptureWindow(wnds[wnds.Length - 1]), imgLeft.Width, imgLeft.Height);
            else
                imgLeft.Image = M_Resize(capt.CaptureWindow(wnds[count - 1]), imgLeft.Width, imgLeft.Height);
            if (count == wnds.Length - 1)
                imgRight.Image = M_Resize(capt.CaptureWindow(wnds[0]), imgRight.Width, imgRight.Height);
            else
                imgRight.Image = M_Resize(capt.CaptureWindow(wnds[count + 1]), imgRight.Width, imgRight.Height); 
        }

        private void Set_Click(object sender, EventArgs e)
        {
            count = listBoxWindow.Items.IndexOf(listBoxWindow.Text);
            my_hwnd = wnds[count];
            img.Image = M_Resize(capt.CaptureWindow(wnds[count]), img.Width, img.Height);
            if(count==0)
                imgLeft.Image = M_Resize(capt.CaptureWindow(wnds[wnds.Length - 1]), imgLeft.Width, imgLeft.Height);
            else
                imgLeft.Image = M_Resize(capt.CaptureWindow(wnds[count - 1]), imgLeft.Width, imgLeft.Height);
            if(count==wnds.Length-1)
                imgRight.Image = M_Resize(capt.CaptureWindow(wnds[0]), imgRight.Width, imgRight.Height);
            else
                imgRight.Image = M_Resize(capt.CaptureWindow(wnds[count+1]), imgRight.Width, imgRight.Height);
            label5.Text = wnd.getText(wnds[count]);
            listBoxWindow.SetSelected(count, true);
        }

        private void hScrollBar1_ValueChanged(object sender, EventArgs e)
        {
            if (scroll_left.Value>= scroll_right.Value)
                scroll_left.Value = scroll_right.Value - 1;
            p_left=scroll_left.Value*System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width/100;
            p_width = (scroll_right.Value - scroll_left.Value) * System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width / 100;
            refresh();
        }

        private void scroll_right_Scroll(object sender, ScrollEventArgs e)
        {
            if (scroll_left.Value >= scroll_right.Value)
                scroll_right.Value = scroll_left.Value + 1;
            p_width = (scroll_right.Value - scroll_left.Value) * System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width / 100;
            refresh();
        }
        
        private void scroll_top_Scroll(object sender, ScrollEventArgs e)
        {
            if (scroll_top.Value >= scroll_bottom.Value)
                scroll_top.Value = scroll_bottom.Value - 1;
            p_top = scroll_top.Value * System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height / 100;
            p_height = (scroll_bottom.Value - scroll_top.Value) * System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height / 100;
            refresh();
        }
        
        private void scroll_bottom_Scroll(object sender, ScrollEventArgs e)
        {
            if (scroll_top.Value >= scroll_bottom.Value)
                scroll_bottom.Value = scroll_top.Value + 1;
            p_height = (scroll_bottom.Value - scroll_top.Value) * System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height / 100;
            refresh();
        }
        
        private void Window_select(object sender, EventArgs e)
        {
            listBoxWindow.Enabled = true;
            RightImage.Enabled = true;
            LeftImage.Enabled = true;
            SetImage.Enabled = true;
            img.Location = new Point(289, 143);
            img.Width = 256;
            img.Height = 200;
            imgLeft.Image = M_Resize(capt.CaptureWindow(wnds[wnds.Length-1]), imgLeft.Width, imgLeft.Height);
            imgRight.Image = M_Resize(capt.CaptureWindow(wnds[1]), imgRight.Width, imgRight.Height);
            img.Image = M_Resize(capt.CaptureWindow(wnds[0]), img.Width, img.Height);
            imgLeft.BorderStyle = BorderStyle.Fixed3D;
            imgRight.BorderStyle = BorderStyle.Fixed3D;
            img.BorderStyle = BorderStyle.Fixed3D;
            scroll_left.Enabled = false;
            scroll_right.Enabled = false;
            scroll_top.Enabled = false;
            scroll_bottom.Enabled = false;
        }

        private void Desktop_select(object sender, EventArgs e)
        {
            listBoxWindow.Enabled = false;
            RightImage.Enabled = false;
            LeftImage.Enabled = false;
            SetImage.Enabled = false;
            img.Location = new Point(160, 43);
            img.Width = 512;
            img.Height = 400;
            scroll_left.Enabled = true;
            scroll_right.Enabled = true;
            scroll_top.Enabled = true;
            scroll_bottom.Enabled = true;
            Color color = Color.Yellow;
            Color semiTransparentColor = Color.FromArgb(128, color.R, color.G, color.B);
            refresh();
        }

        private void imgRight_Click(object sender, EventArgs e)
        {

        }
    }
}
